<template>
  <div></div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: 'chart-line',
  props: {
    id: {
      type: String,
      default: ''
    },
    data: {
      type: Object,
      default: () => {}
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      lineChart: {}
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.lineChartInit()
    })
  },
  beforeDestroy() {
    if (!this.lineChart) {
      return
    }
    this.lineChart.dispose()
    this.lineChart = null
  },
  watch: {
    // 监听数据变化
    data: {
      handler(newVal, oldVal) {
        if (this.lineChart) {
          if (newVal) {
            this.data = newVal
            this.lineChartInit()
          } else {
            this.data = oldVal
            this.lineChartInit()
          }
        } else {
          this.$nextTick(() => {
            this.lineChartInit()
          })
        }
      },
      deep: true // 对象内部属性的监听，关键。
    }
  },
  methods: {
    lineChartInit() {
      this.lineChart = echarts.init(document.getElementById(this.id))

      const lineChartData = {
        data: this.data.datas,
        name: this.data.name,
        time: this.data.time
      }

      this.lineChart.setOption({
        title: {
          text: this.title,
          left: 'center',
          backgroundColor: '#62AFE5',
          textStyle: {
            color: '#fff',
            fontSize: 16
          }
        },
        xAxis: {
          name: '月份',
          type: 'category',
          data: lineChartData.time,
          axisTick: {
            show: false // 是否显示刻度
          },
          // x轴的颜色和宽度
          axisLine: {
            lineStyle: {
              color: '#A7A7A7', // x坐标轴的轴线颜色
              width: 1 // 这里是坐标轴的宽度,可以去掉
            }
          }
        },
        yAxis: {
          name: '销量',
          type: 'value',
          axisTick: {
            show: false // 是否显示刻度
          },
          nameTextStyle: {
            color: '#62AFE5',
            fontSize: 14
          },
          // y轴的颜色和宽度
          axisLine: {
            lineStyle: {
              color: '#A7A7A7', // y坐标轴的轴线颜色
              width: 1 // 这里是坐标轴的宽度,可以去掉
            }
          },
          // 控制网格线是否显示
          splitLine: {
            show: true, // 网格线是否显示
            //  改变样式
            lineStyle: {
              color: '#A7A7A7', // 修改网格线颜色
              type: 'dashed'
            }
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          formatter: function(params) {
            return params[0].data + '本'
          }
        },
        series: [
          {
            name: lineChartData.name,
            symbolSize: 8,
            data: lineChartData.data,
            type: 'line',
            itemStyle: {
              color: 'rgba(34, 173, 255, 1)',
              normal: {
                label: {
                  show: true
                }
              }
            }
          }
        ]
      })
      window.addEventListener('resize', () => {
        if (this.lineChart) {
          this.lineChart.resize()
        }
      })
    }
  }
}
</script>

<style></style>
